﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

/// <summary>
///ShuoMing 的摘要说明
/// </summary>
public partial class ShuoMing
{
    public enum typeYH { 家教 = 1, 个人培训, 企业培训 }
    public static ShuoMing GetByID(int id)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.ShuoMing.FirstOrDefault(row => row.ID == id);
        }
    }

    public static List<ShuoMing> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.ShuoMing.ToList();
        }
    }

    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.ShuoMing.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<ShuoMing>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var objItem = context.ShuoMing.FirstOrDefault(row => row.ID == id);
            if (objItem == null)
                return;
            context.ShuoMing.DeleteOnSubmit(objItem);
            context.SubmitChanges();
        }
    }

    public static List<ShuoMing> GetAllByConditon(int userID, int typeYH, string title, string des)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from t in context.ShuoMing
                        select t;
            if (userID != 0)
                query = query.Where(row => row.AddUserID == userID);
            if (typeYH != 0)
                query = query.Where(row => row.Type == typeYH);
            if (!string.IsNullOrEmpty(title))
                query = query.Where(row => row.Title.Contains(title));
            if (!string.IsNullOrEmpty(des))
                query = query.Where(row => row.Des.Contains(des));
            query = query.OrderByDescending(row => row.Rank);
            return query.ToList();
        }
    }
}